package com.itcast.activiti.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.activiti.entity.ApproveField;
import com.itcast.activiti.mapper.ApproveFieldMapper;
import com.itcast.activiti.service.IApproveFieldService;
import com.itcast.activiti.utils.ItcastPage;
import com.itcast.activiti.utils.VariablesUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 变量表  服务实现类
 * </p>
 *
 * @author itcast
 * @since 2021-03-02
 */
@Service
public class ApproveFieldServiceImpl extends ServiceImpl<ApproveFieldMapper, ApproveField> implements IApproveFieldService {

    @Override
    public ItcastPage listApproveFieldPage(ItcastPage itcastPage) {
        JSONObject condition = itcastPage.getCondition();
        QueryWrapper<ApproveField> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(condition.getString(ApproveField.Fields.fieldKey))) {
            queryWrapper.lambda().like(ApproveField::getFieldKey, condition.getString(ApproveField.Fields.fieldKey));
        }
        if (StringUtils.hasText(condition.getString(ApproveField.Fields.name))) {
            queryWrapper.lambda().like(ApproveField::getName, condition.getString(ApproveField.Fields.name));
        }
        queryWrapper.lambda().eq(ApproveField::getDeleted, false).orderByDesc(ApproveField::getCreatedDatetime);
        return baseMapper.selectPage(itcastPage, queryWrapper);
    }

    @Override
    public List<ApproveField> listApproveField() {
        QueryWrapper<ApproveField> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApproveField::getDeleted, false).orderByDesc(ApproveField::getCreatedDatetime);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public ApproveField getApproveField(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean addApproveField(ApproveField approveField) {
        Assert.isTrue(VariablesUtil.checkInput(approveField.getFieldKey()), "不允许中文，只允许字母+部分特殊符号/数字");
        Assert.isTrue(!checkApproveFieldByFieldKey(approveField.getId(), approveField.getFieldKey()), approveField.getFieldKey() + " is exist");
        int insert = baseMapper.insert(approveField);
        return insert == 1;
    }

    @Override
    public boolean editApproveField(ApproveField approveField) {
        Assert.isTrue(VariablesUtil.checkInput(approveField.getFieldKey()), "不允许中文，只允许字母+部分特殊符号/数字");
        Assert.isTrue(!checkApproveFieldByFieldKey(approveField.getId(), approveField.getFieldKey()), approveField.getFieldKey() + " is exist");
        int insert = baseMapper.updateById(approveField);
        return insert == 1;
    }

    // true 已存在
    private boolean checkApproveFieldByFieldKey(Integer id, String fieldKey) {
        Assert.hasText(fieldKey, "fieldKey is empty");
        ApproveField approveFieldOld = baseMapper.selectOne(new QueryWrapper<ApproveField>().lambda().eq(ApproveField::getFieldKey, fieldKey));
        if (id == null) {
            return approveFieldOld != null;
        }
        if (approveFieldOld == null) {
            return false;
        }
        if (!id.equals(approveFieldOld.getId()) && approveFieldOld.getFieldKey().equals(fieldKey)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean delApproveField(Integer id, String deletedBy) {
        ApproveField approveField = new ApproveField();
        approveField.setId(id);
        approveField.setDeletedBy(deletedBy);
        approveField.setDeletedDatetime(LocalDateTime.now());
        approveField.setDeleted(true);
        int i = baseMapper.updateById(approveField);
        return i == 1;
    }
}
